package Math;

/* Start of 'Dictionary' class */
public class Dictionary
{
  private Stock Keys;           // Stock of Keystificators
  private Stock Elems;           // Stock of elements

  /* 'Dictionary' class constructor
   * ARGUMENTS: None.
   * RETURNS: None.
   */
  public Dictionary( )
  {
    Keys = new Stock(2);
    Elems = new Stock(2);
  } /* End of constructor function */


  /* Add element and it's Keystficator
   * ARGUMENTS:
   *   - adding element key object
   *       Object Key;
   *   - element for edding
   *       Object Elem;
   * RETURNS:
   *   - self reference
   *       Dictionary;
   */
  public Dictionary Add( Object Key, Object Elem )
  {
    for (int i = 0; i < Keys.Size(); i++)
      if (EqualObjects(Key, Keys.Get(i)))
      {
        Elems.Set(i, Elem);
        return this;
      }

    Keys.Add(Key);
    Elems.Add(Elem);

    return this;
  } /* End of 'Add' function */


  /* Get element by his key function
   * ARGUMENTS:
   *   - element`s key
   *       Object Key;
   * RETURNS:
   *   - founded object, or 'null' if object not found
   *       Object;
   */
  public Object Get( Object Key )
  {
    for(int i = 0; i < Keys.Size(); i++)
      if (EqualObjects(Key, Keys.Get(i)))
        return Elems.Get(i);
    return null;
  } /* End of 'Get' function */

  /* Get element by his position in dictionary function
   * ARGUMENTS:
   *   - element position
   *       int Pos;
   * RETURNS:
   *   - element from dictionary
   *       Object;
   */
  public Object GetByPos( int Pos )
  {
    return Elems.Get(Pos);
  } /* End of 'GetByPos' function */

  /* Get dictionary size function
   * ARGUMENTS: None.
   * RETURNS:
   *   - dictionary size
   *       int;
   */
  public final int Size( )
  {
    return Keys.Size();
  } /* End of 'Size' function */


  /* Delete element by his key
   * ARGUMENTS:
   *   - element`s key
   *       Object Key;
   * RETURNS:
   *   - self reference
   *       Dictionary;
   */
  public Dictionary Delete( Object Key )
  {
    for(int i = 0; i < Keys.Size(); i++)
      if (Key == Keys.Get(i))
      {
        Keys.DeleteNum(i);
        Elems.DeleteNum(i);
        break;
      }

   return this;
  } /* End of 'Delete' function */

  /* Get all object`s keys function
   * ARGUMENTS: None.
   * RETURNS:
   *   - all object`s keys
   *       Object[];
   */
  public Object[] GetAllKeys( )
  {
    return Keys.GetAll();
  } /* End of 'GetAllKeys' function */


  /* Free dictionary function
   * ARGUMENTS: None.
   * RETURNS:
   *   - self reference
   *       Dictionary;
   */
  public Dictionary Free( )
  {
    Keys.Free();
    Elems.Free();

    return this;
  } /* End of 'Free' function */

  /* Is objects equal function
   * ARGUMENTS:
   *   - two objects
   *       Object A, B;
   * RETURNS:
   *   - 'true' if objects are equal, 'false' - otherwice
   *       boolean;
   */
  private boolean EqualObjects( Object A, Object B )
  {
    if (A == B)
      return true;
    if (A.getClass() != B.getClass())
      return false;
    if (A.equals(B))
      return true;
   
    return false;
  } /* End of 'EqualObjects' function */

} /* End of 'Dictionary' class */
